define(function () {
    require('../../less/bottomaction.less');
    function BottomAction(para) {
        var testStyle = document.createElement("DIV").style;

        if ("-webkit-transform" in testStyle) {
            this.transitionend = "webkitTransitionEnd";
            this.transform = "-webkit-transform";
            this.transition = "-webkit-transition";
        } else {
            this.transitionend = "transitionend";
            this.transform = "transform";
            this.transition = "transition";
        }

        this.container = document.body;

        if (para) {
            if (para.container) {
                this.container = para.container;
            }
        }

        this._initLayout();

        this.callbacks = {};
    }

    BottomAction.prototype = {
        _createElementWithClass: function (tagName, className) {
            var classList = className.split(" ");
            var ele = document.createElement(tagName);
            for (var i = 0, j = classList.length; i < j; i++) {
                if (/^\s*$/.test(classList[i])) {
                    continue;
                }
                ele.classList.add(classList[i]);
            }
            return ele;
        },
        hide: function () {
            this.BA_BK.classList.remove("ba-bk-show");
            this.BA_Dialog.classList.remove("ba-dialog-show");
            return this;
        },

        //需要传模板数据
        show: function (tpl) {
            this.setValue(tpl);

            var _this = this;

            window.setTimeout(function () {
                _this.BA_BK.classList.add("ba-bk-show");
                _this.BA_Dialog.classList.add("ba-dialog-show");
            }, 0);

            this.bindSelectEvent();

            return this;
        },
        setValue: function (tpl) {
            if (!this.contentContainer) {
                this.contentContainer = this._createElementWithClass('DIV', 'content-container');
            }

            this.contentContainer.innerHTML = tpl;
            this.BA_Dialog.appendChild(this.contentContainer);
        },
        _initLayout: function () {
            if (!this.BA_BK || !this.BA_Dialog) {

                this.BA_BK = this._createElementWithClass("DIV", "ba-bk");
                this.BA_Dialog = this._createElementWithClass("DIV", "ba-dialog");
                this.container.appendChild(this.BA_BK);
                this.container.appendChild(this.BA_Dialog);
                var _this = this;
                this.BA_BK.addEventListener("click", function (e) {
                    _this.hide();
                }, false);
            }
        },
        bindSelectEvent: function () {
            var _this = this;

            this.contentContainer.addEventListener('click', function () {
                _this.hide();
            });
        },
        Destory: function () {
            //移除事件 销毁DOM
            this.BA_BK && this.container.removeChild(this.BA_BK);
            this.BA_Dialog && this.container.removeChild(this.BA_Dialog);
            this.BA_Dialog = null;
            this.BA_BK = null;
        }
    };
    return BottomAction;

});